package org.jflame.commons.json;

import java.util.Objects;
import java.util.TimeZone;

/**
 * json通用配置
 * 
 * @author charles.zhang
 */
public class JsonCfg {

    private boolean ignoreNull;
    private boolean enableAutoTypeSupport;
    private boolean prettyFormat;
    private boolean browserCompatible;
    private boolean bigDecimalAsPlain;
    private String dateFormat;
    private TimeZone timeZone;
    private Integer namingStrategy;

    public JsonCfg() {
        this(true, false, false, null, null, null, false, true);
    }

    public JsonCfg(boolean ignoreNull, boolean prettyFormat, boolean browserCompatible, String dateFormat,
            TimeZone timeZone, Integer namingStrategy, boolean enableAutoTypeSupport, boolean bigDecimalAsPlain) {
        this.ignoreNull = ignoreNull;
        this.enableAutoTypeSupport = enableAutoTypeSupport;
        this.prettyFormat = prettyFormat;
        this.browserCompatible = browserCompatible;
        this.dateFormat = dateFormat;
        this.timeZone = timeZone;
        this.namingStrategy = namingStrategy;
        this.bigDecimalAsPlain = bigDecimalAsPlain;
    }

    public boolean isIgnoreNull() {
        return ignoreNull;
    }

    public JsonCfg ignoreNull(boolean ignoreNull) {
        this.ignoreNull = ignoreNull;
        return this;
    }

    public boolean isEnableAutoTypeSupport() {
        return enableAutoTypeSupport;
    }

    public JsonCfg enableAutoTypeSupport(boolean enableAutoTypeSupport) {
        this.enableAutoTypeSupport = enableAutoTypeSupport;
        return this;
    }

    public boolean isPrettyFormat() {
        return prettyFormat;
    }

    public JsonCfg prettyFormat(boolean prettyFormat) {
        this.prettyFormat = prettyFormat;
        return this;
    }

    public String getDateFormat() {
        return dateFormat;
    }

    public JsonCfg dateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
        return this;
    }

    /*public String getDateTimeFormat() {
        return dateTimeFormat;
    }
    
    public JsonCfg setDateTimeFormat(String dateTimeFormat) {
        this.dateTimeFormat = dateTimeFormat;
        return this;
    }
    
    public String getTimeFormat() {
        return timeFormat;
    }
    
    public JsonCfg setTimeFormat(String timeFormat) {
        this.timeFormat = timeFormat;
        return this;
    }
    
    public JsonCfg setTimeFormat(String dateTimeFormat, String dateFormat, String timeFormat) {
        this.dateTimeFormat = dateTimeFormat;
        this.dateFormat = dateFormat;
        this.timeFormat = timeFormat;
        return this;
    }*/

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public JsonCfg timeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
        return this;
    }

    public JsonCfg timeZone(String timeZoneID) {
        this.timeZone = TimeZone.getTimeZone(timeZoneID);
        return this;
    }

    public boolean getBrowserCompatible() {
        return browserCompatible;
    }

    public JsonCfg browserCompatible(boolean browserCompatible) {
        this.browserCompatible = browserCompatible;
        return this;
    }

    public Integer getNamingStrategy() {
        return namingStrategy;
    }

    /**
     * 设置属性名称策略:
     * <ul>
     * <li>1=CamelCase 驼峰命名,首字母小写,单词首字母大写,如:userName</li>
     * <li>2=PascalCase 大驼峰命名,单词首字母大写,如:UserName</li>
     * <li>3=SnakeCase 蛇形命名,小写,单词间下划线分隔,如:user_name</li>
     * <li>4=KebabCase 小写,单词间中划线分隔,如:user-name</li>
     * <ul>
     * 
     * @param strategy
     */
    public JsonCfg namingStrategy(int namingStrategy) {
        this.namingStrategy = namingStrategy;
        return this;
    }

    public boolean isBigDecimalAsPlain() {
        return bigDecimalAsPlain;
    }

    public JsonCfg bigDecimalAsPlain(boolean bigDecimalAsPlain) {
        this.bigDecimalAsPlain = bigDecimalAsPlain;
        return this;
    }

    /*public void addChangeListener(PropertyChangeListener changeListener) {
        propChangeSupport.addPropertyChangeListener(changeListener);
    }*/

    @Override
    public int hashCode() {
        return Objects.hash(browserCompatible, dateFormat, enableAutoTypeSupport, ignoreNull, namingStrategy,
                prettyFormat, timeZone.getID());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        JsonCfg other = (JsonCfg) obj;
        return browserCompatible == other.browserCompatible && Objects.equals(dateFormat, other.dateFormat)
                && enableAutoTypeSupport == other.enableAutoTypeSupport && ignoreNull == other.ignoreNull
                && Objects.equals(namingStrategy, other.namingStrategy) && prettyFormat == other.prettyFormat
                && Objects.equals(timeZone, other.timeZone);
    }

}
